import $ from '../common';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    verify: "",
    unionid: "",
    submitText: "绑定",
    disabledCode: false,
    areaCode: "+86",
    unionid: '',
    type: ''
  },

  onLoad: function (options) {
    let {
      type,
      unionid = '',
      wx_nickname = ''
    } = options
    this.setData({
      submitText: type == 'bind' ? '绑定' : '登录',
      unionid,
      type,
      wx_nickname
    })
  },
  /**
   * 表单提交
   */
  submit(e) {
    let value = e.detail.value
    if (!value.user || !value.verify) {
      $.tip("手机号或验证码不能为空！")
      return false
    }
    if (this.data.areaCode == '+86') {
      this.setData({
        phoneNumber: this.data.phone.replace(/\s/g, "")
      })
    } else {
      this.setData({
        phoneNumber: this.data.areaCode + this.data.phone.replace(/\s/g, "")
      })
    }
    $.post('/user/bind/unionid', {
      phone: this.data.phoneNumber,
      verifycode: value.verify,
      unionid: this.data.unionid,
      wx_nickname: this.data.wx_nickname,
      client: 'kbsq'
    }, data => {
      wx.removeStorageSync('$alreadyLoginWindow')
      wx.setStorageSync('token', data.token)
      wx.setStorageSync('uuid', data.uuid)
      wx.setStorageSync('phone', this.data.phoneNumber)
      wx.setStorageSync('$Reload_page', "yes")
      let pull = $.parent().onPullDownRefresh
      pull && pull()
      $.cmp_uuid((campus_uuid) => {
        if (!campus_uuid) {
          return false
        }
        if (this.data.type == "open") {
          wx.navigateBack()
        } else {
          wx.switchTab({
            url: '/pages/index/index'
          })
        }
      })
    })
  },
  /**
   * 重置手机号
   */
  resetphone() {
    this.data.phone = ""
    this.setData({
      phone: ""
    })
  },
  bindPhone(e) {
    var phone = this.data.phone
    phone = e.detail.value.replace(/\s/g, "")
    this.setData({
      phone
    })
  },
  countdown: 60,
  /**
   * 重置验证码
   */
  resetDentify() {
    clearInterval(this.setTimeDentify)
    this.disabledCode = false
    this.countdown = 60
    this.setData({
      countdown: this.countdown,
      disabledCode: this.disabledCode
    })
  },
  /**
   * 获取验证码
   */
  getCode() {
    if (!this.data.phone) {
      $.tip("手机号不能为空！")
      return false
    }
    let countdown = this.countdown
    let _fn = () => {
      if (countdown == 0) {
        this.resetDentify()
      } else {
        this.disabledCode = true
        this.setData({
          countdown: countdown,
          disabledCode: this.disabledCode
        })
        if (countdown == 60) {
          this.getIdentify()
        }
        countdown--
      }
    }
    _fn()
    this.setTimeDentify = setInterval(() => {
      _fn()
    }, 1000)
  },
  getIdentify() {
    if (this.data.areaCode == '+86') {
      this.setData({
        phoneNumber: this.data.phone.replace(/\s/g, "")
      })
    } else {
      this.setData({
        phoneNumber: this.data.areaCode + this.data.phone.replace(/\s/g, "")
      })
    }
    $.post("/verifycode", {
      phone: this.data.phoneNumber,
      client: 'kbsq',
      channel: '开班神器'
    }, (data) => {
      $.tip("验证码已发送到尾号为" + (this.data.phone).replace(/\s/g, "").toString().substring(7) + "的手机上，请注意查收!")
    }, (res) => {
      $.tip(res.message)
      this.resetDentify()
    })
  },
  /**
   * 选择区号
   */
  select() {
    wx.navigateTo({
      url: '../../packageA/login/selectcode',
    })
  },
})